package tag

import (
	"time"
)

// TypeArticle 文章标签
const TypeArticle = 1

// TypeBook 书籍标签
const TypeBook = 2

// Tag 标签实体
type Tag struct {
	ID          uint      `json:"id" gorm:"primaryKey;column:id"`
	Name        string    `json:"name" gorm:"column:name;size:50;not null;index:idx_tags_name_type,unique,priority:1"`
	Slug        string    `json:"slug" gorm:"column:slug;size:100;not null;index:idx_tags_slug_type,unique,priority:1"`
	Type        int       `json:"type" gorm:"column:type;not null;index;index:idx_tags_name_type,unique,priority:2;index:idx_tags_slug_type,unique,priority:2;comment:1=文章标签,2=书籍标签"`
	Description string    `json:"description" gorm:"column:description;size:200"`
	UsageCount  int64     `json:"usage_count" gorm:"column:usage_count;default:0;comment:被使用的次数"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at;not null;autoCreateTime"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at;not null;autoUpdateTime"`
}

// TableName 指定表名
func (Tag) TableName() string {
	return "tags"
}

// NewTag 创建一个新的标签
func NewTag(name, slug, description string, tagType int) *Tag {
	now := time.Now()
	return &Tag{
		Name:        name,
		Slug:        slug,
		Type:        tagType,
		Description: description,
		UsageCount:  0,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
}

// IsArticleTag 判断是否为文章标签
func (t *Tag) IsArticleTag() bool {
	return t.Type == TypeArticle
}

// IsBookTag 判断是否为书籍标签
func (t *Tag) IsBookTag() bool {
	return t.Type == TypeBook
}

// IsValidType 判断类型是否有效
func IsValidType(tagType int) bool {
	return tagType == TypeArticle || tagType == TypeBook
}
